<template>
  <el-dialog
    :title="title"
    :visible.sync="open"
    width="560px"
    append-to-body
    @close="closeDialog"
    :close-on-click-modal="false"
  >
    <el-form :model="info" ref="queryForm" label-width="100px" size="small" :rules="rules" class="pr40">
      <el-form-item label="语料名称" prop="name">
        <el-input v-model="info.name" placeholder="" clearable maxlength="50" />
      </el-form-item>
      <!-- <el-form-item label="分类" prop="classifyId">
        <el-select style="width: 100%">
        </el-select>
      </el-form-item> -->
      <el-form-item label="类型" prop="languageType">
        <el-select v-model="info.languageType" style="width: 100%">
          <el-option label="中文" value="zh" />
          <el-option label="中英文" value="both" />
        </el-select>
      </el-form-item>
      <el-form-item label="描述" prop="intro">
        <el-input type="textarea" v-model="info.intro" placeholder="" :rows="6" maxlength="200"></el-input>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer pr40">
      <el-button size="small" @click="open = false">取消</el-button>
      <el-button type="primary" size="small" @click="handleSubmit" :loading="loading">确定</el-button>
    </div>

    <UserSelectionModal :visible.sync="userSelectionModalVisible" @onChange="onUserSelected" />
  </el-dialog>
</template>
<script>
import { Message } from 'element-ui'
import { handleBlockInfo, addBlockInfo } from '@/api/business'
import UserSelectionModal from './UserSelectionModal.vue'
import { cloneDeep } from 'lodash'

export default {
  name: 'BlockEditModal',
  components: { UserSelectionModal },
  props: {
    editData: {
      type: Object
    },
    visible: {
      type: Boolean,
      default: false
    },
    selectedNode: {
      type: Object,
      default: () => ({})
    }
  },
  watch: {
    visible(val) {
      this.open = val
      if (val) {
        this.$nextTick(() => {
          this.$refs.queryForm?.clearValidate()
          this.title = this.editData?.id ? '编辑结构化语料' : '新增结构化语料'
        })
      }
    },
    editData(val) {
      const _val = val || {
        languageType: 'zh',
        name: '',
        intro: ''
      }
      this.info = cloneDeep(_val)
      console.log('this.info =', this.info)
    }
  },
  data() {
    return {
      open: this.visible,
      info: {
        languageType: 'zh',
        name: '',
        intro: ''
      },
      title: '新增结构化语料',
      rules: {
        name: [
          { required: true, message: '请输入结构化内容块名称', trigger: 'change' },
          { pattern: /\s*\S+?/, message: '结构化内容块名称不能为空', trigger: 'blur' }
        ],
        // intro: [
        //   { required: true, message: '请输入描述信息', trigger: 'change' },
        //   { pattern: /\s*\S+?/, message: '描述信息不能为空', trigger: 'change' }
        // ],
        languageType: [{ required: true, message: '请选择类型', trigger: 'change' }]
      },
      loading: false,
      userSelectionModalVisible: false
    }
  },
  methods: {
    closeDialog() {
      this.info = {
        languageType: 'zh',
        name: '',
        intro: ''
      }
      this.$emit('update:visible', false)
      this.$emit('onClose')
    },
    handleSubmit() {
      this.$refs.queryForm.validate((valid) => {
        if (valid) {
          let params = JSON.parse(JSON.stringify(this.info))
          params.classifyId = this.editData?.name ? params.classifyId : this.selectedNode.id
          if (this.editData?.name) {
            params.id = this.editData.id
          }
          const call = this.editData?.name ? handleBlockInfo : addBlockInfo
          this.loading = true
          call(params)
            .then((res) => {
              this.open = false
              Message.success('保存成功')
              this.$emit('onSuccess')
            })
            .finally(() => {
              this.loading = false
            })
        }
      })
    },
    handleShowUserDialog() {
      this.userSelectionModalVisible = true
    },
    onUserSelected(selectedUser) {
      this.info.dutyUserName = selectedUser.userName
      this.info.dutyUserId = selectedUser.userId
    }
  }
}
</script>
